import { dayjs, FormItemRule } from "element-plus"
import CellBtns from "../components/CellBtns.vue"
import { h } from "vue"

export const isFunc = (v: any) => typeof v === 'function'

export const isBool = (v: any) => typeof v === 'boolean'

export const isArr = (v: any) => v instanceof Array

export const genKey = ((k = 1) => () => k++)()

export const createNonEmptyRule = (message: string): FormItemRule => {
  return {
    message,
    pattern: /\S+/,
    required: true,
    trigger: 'blur'
  }
}

export const createRequiredRule = (message: string): FormItemRule => {
  return {
    message,
    required: true
  }
}

export const callFunc = <T extends (...args: any) => any>(fn?: T, ...args: Parameters<T>) => fn && fn(...args)

export const hBtns = (btns: InstanceType<typeof CellBtns>['$props']['btns']) => h(CellBtns, { btns })

export const fmtDate = (date: dayjs.ConfigType, tpl = 'YYYY-MM-DD HH:mm:ss') => dayjs(date).format(tpl)
